package example.beans;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import example.model.Request;

import java.io.Serializable;
import java.sql.*;

public class RequestBean implements Serializable{	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List <Request>requests;
 
	public List <Request>getRequests() {
		return requests;
	}
	
	private Request firstRequest;

	public Request getFirstRequest() {
		init();
		return firstRequest;
	}
		
	
 @PostConstruct
 	public void init () {  
	 requests = new ArrayList <Request>();
  
		  Connection con = null;
		  try {
		   //Class.forName("com.mysql.jdbc.Driver");
		   String url = "jdbc:mysql://localhost:3306/dbagatha";
		   con = DriverManager.getConnection(url, "root","12345");
		   Statement st = con.createStatement();
		   																						//parent IS NULL
		   ResultSet rsF = st.executeQuery("select * from request, project, auther where (request.id = 16) and (request.idproject = project.id) and (request.idauther = auther.id)");
		   while (rsF.next()) {

			   requests.add (new Request(rsF.getString(1).toString(), rsF.getString(2).toString(), rsF.getString(3), rsF.getString(4), rsF.getString(5).toString(), rsF.getString(8).toString(), rsF.getString(13), rsF.getString(18)));
		   }
		  } catch (Exception e) {
		   e.printStackTrace();
		   System.out.println("Exception: " + e.getMessage());
		  } finally {
		   try {
		    if (con != null)
		     con.close();
		   } catch (SQLException e) {
		    
		   }
		  }
		  
 		rowsToUpdate =  new HashSet<Integer>();  
 		
 		firstRequest = requests.get(0);

 	} 

 @PostConstruct 
	public  List<Request> GetChildren () {  
	 requests = new ArrayList <Request>();
  
		  Connection con = null;
		  try {
		   String url = "jdbc:mysql://localhost:3306/dbagatha";
		   con = DriverManager.getConnection(url, "root","12345");
		   Statement st = con.createStatement();
		   String identificator= parentRequest.id;		   
		   
		   ResultSet rsChild = st.executeQuery("select * from request, project, auther where (request.idparent =" + identificator + ") and (request.idproject = project.id) and (request.idauther = auther.id)");
		   while (rsChild.next()) {

			   requests.add (new Request(rsChild.getString(1).toString(), rsChild.getString(2).toString(), rsChild.getString(3), rsChild.getString(4), rsChild.getString(5).toString(), rsChild.getString(8).toString(),rsChild.getString(13), rsChild.getString(18)));
		   }
		  } catch (Exception e) {
		   e.printStackTrace();
		   System.out.println("Exception: " + e.getMessage());
		  } finally {
		   try {
		    if (con != null)
		     con.close();
		   } catch (SQLException e) {
		    
		   }
		  }				  
 		rowsToUpdate =  new HashSet<Integer>();
 		return requests;
 	} 
 
 
	private Request selectedRequest;	

	public Request getSelectedRequest() {
		return selectedRequest;
	}
	public void setSelectedRequest(Request selectedRequest) {
		this.selectedRequest = selectedRequest;
	}

	private Request parentRequest;	
	
	public Request getParentRequest() {
		return parentRequest;
	}
	public void setParentRequest(Request parentRequest) {
		this.parentRequest = parentRequest;
	}
	
	private Set <Integer>rowsToUpdate;
	
	public Set <Integer>getRowsToUpdate() {
		return rowsToUpdate;
	}
	public void save (ActionEvent event) {	  
		rowsToUpdate.clear();
		rowsToUpdate.add(requests.indexOf(selectedRequest));
	} 
	
	public void showChildren(ActionEvent event) {
		GetChildren();
	}
	
 	private DataSource dataSource;

 	public void Database(String jndiname) {
 		try {
 			dataSource = (DataSource) new InitialContext().lookup("java:comp/env/" + "dbagatha");
 		} 
 		catch (NamingException e) {
 		}
 	}

 	public Connection getConnection() throws SQLException {
 		return dataSource.getConnection();
 	}


}  

